package view;

import com.wedu.entity.Job;
import com.wedu.entity.User;
import com.wedu.service.UserService;
import com.wedu.util.TSUtility;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


//兼职人员
public class Part_time_menu {
    private static Scanner sc=new Scanner(System.in);
    static void Part_timeStaffMenu(User u, UserService userService) {
        System.out.println("**************************");
        System.out.println("***                    ***");
        System.out.println("***  欢迎"+u.getName()+"兼职人员  ***");
        System.out.println("***                    ***");
        System.out.println("**************************");
        System.out.println("\t\t1.个人信息管理\n\t\t2.兼职申请\n\t\t3.退出登录\n\t\t4.退出系统");
        System.out.print("请输入您的选择：");
        while (true){
            char c= TSUtility.readMenuSelection();
            switch (c){
                case '1':
                    //个人信息管理
                    PIM(u,userService);
                    break;
                case '2':
                    //兼职申请
                    apply(u,userService);
                    break;
                case '3':
                    TSUtility.loadSpecialEffects();
                    System.out.println("退出成功！");
                    return;
                case '4':
                    System.out.print("你确定要退出系统吗？请输入（Y/N）：");
                    char a=TSUtility.readConfirmSelection();
                    TSUtility.loadSpecialEffects();
                    if (a=='Y'||a=='y'){
                        System.out.println("退出成功！欢迎下次使用！");
                        System.exit(0);
                    }else {
                        System.out.println("你选择了NO，未退出系统，欢迎继续使用！");
                        System.out.println("\t\t1.个人信息管理\n\t\t2.兼职申请\n\t\t3.退出登录\n\t\t4.退出系统");
                        System.out.print("请输入您的选择：");
                        break;
                    }
            }
        }
    }

    //个人信息管理
    private static void PIM(User user,UserService userService){
        TSUtility.loadSpecialEffects();
        System.out.println("...........个人信息管理............");
        System.out.println("1.个人信息修改\t\t\t2.查询用户等级");
        System.out.println("3.查询余额    \t\t\t4.返回上一级目录");
        System.out.print("请输入您的的选择：");
        while (true) {
            char c = TSUtility.readMenuSelection();
            switch (c) {
                case '1':
                    //个人信息修改
                    PIMModify(user,userService);
                    break;
                case '2':
                    //查询用户信誉等级
                    queryLevel(user,userService);
                    break;
                case '3':
                    checkBalance(user,userService);
                    //查询余额
                    break;
                case '4':
                    System.out.println("\t\t1.个人信息管理\n\t\t2.兼职申请\n\t\t3.退出登录\n\t\t4.退出系统");
                    System.out.print("请输入您的选择：");
                    return;
            }
        }
    }

    //个人信息修改
    private static void PIMModify(User user,UserService userService){
        System.out.println("..............个人信息修改..............");
        System.out.println("提示：用户只能修改部分属性！其中信誉等级会根据你是否按时完成工作变化，按时完成工作次数和未\n按时完成次数" +
                "累计到三次都会从零开始累计，信誉积分随其增减一。");
        int frequency = userService.frequency(user);
        int frequency_t = userService.frequency_t(user);
        System.out.println("学号（教师编号）："+user.getUid()+"，账户编号："+user.getACID()+"，姓名："+user.getName()
        +"，密码:"+user.getPassword()+"，\n年龄:"+user.getAge()+"，电话:"+user.getPhone()+"，所在院校："+user.getDepartment()
        +"，注册日期："+user.getDate()+"\n按时完成兼职次数："+frequency+"，逾期兼职次数："+frequency_t);
        System.out.println("提示：以下信息全部为可不填，不填时信息未修改");
        int age=0;
        for (;;) {
            System.out.print("请输入新的年龄(18~60)：");
            String s=sc.nextLine();
            if (s.equals(""))
                break;
            try {
                age = Integer.parseInt(s);
                if (age<18||age>60){
                    System.out.println("请正确输入年龄！");
                }else break;
            }catch (NumberFormatException e){
                System.out.println("请正确填写年龄！");
            }
        }
        String sex;
        do {
            System.out.print("请输入你的性别（男|女）：");
            sex = sc.nextLine();
        } while (!sex.equals("男") && !sex.equals("女")&&!sex.equals(""));
        System.out.print("请输入新的密码：");
        String password=sc.nextLine();
        System.out.print("请输入您的联系电话：");
        String phone;
        for (;;) {
            phone= sc.nextLine();
            if (phone.equals(""))
                break;
            if (phone.length()!=11){
                System.out.print("电话长度不正确！请重新输入：");
            }else break;
        }
        System.out.print("请输入您所在的院系：");
        String department=sc.nextLine();
        if (age==0&&sex.equals("")&&password.equals("")&&phone.equals("")&&department.equals("")){
            System.out.println("信息未修改！\n");
        }else {
            System.out.print("你确定修改信息吗？请输入（Y/N）：");
            char a = TSUtility.readConfirmSelection();
            TSUtility.loadSpecialEffects();
            if (a == 'Y' || a == 'y') {
                if (age!=0)
                    user.setAge(age);
                if (!sex.equals(""))
                    user.setSex(sex);
                if (!password.equals(""))
                    user.setPassword(password);
                if (!phone.equals(""))
                    user.setPhone(phone);
                if (!department.equals(""))
                    user.setDepartment(department);
                boolean modify = userService.modify(user);
                if (modify){
                    System.out.println("修改成功！\n");
                }else {
                    System.out.println("修改失败！\n");
                }
            }else {
                System.out.println("你选择了NO，信息未修改！\n");
            }
        }
        //回车继续
        TSUtility.readReturn();
        System.out.println("1.个人信息修改\t\t\t2.查询用户等级");
        System.out.println("3.查询余额    \t\t\t4.返回上一级目录");
        System.out.print("请输入您的的选择：");
    }

    //用户信誉等级查询
    private static void queryLevel(User user,UserService userService){
        System.out.println("...............信誉等级查询...............");
        //System.out.println("信誉等级最高为5级！");
        if (user.getCredit_rating()<5){
            System.out.println("提示：默认未逾期完成兼职工作3次提升1级信用等级，等级梯度为1-5级，5级最高；\n" +
                    "如果逾期兼职工作3次降低1级信用等级，最低为一级，如果降为0级，将列入黑名单！");
        }
        int level = userService.level(user);
        System.out.println("\n你的信誉等级为："+level);
        //回车继续
        TSUtility.readReturn();
        System.out.println("1.个人信息修改\t\t\t2.查询用户等级");
        System.out.println("3.查询余额    \t\t\t4.返回上一级目录");
        System.out.print("请输入您的的选择：");
    }

    //余额查询
    private static void  checkBalance(User u,UserService userService){
        System.out.println("...............余额查询...............");
        double balance = userService.balance(u);
        System.out.println("你的余额为："+balance+"元！");
        //回车继续
        TSUtility.readReturn();
        System.out.println("1.个人信息修改\t\t\t2.查询用户等级");
        System.out.println("3.查询余额    \t\t\t4.返回上一级目录");
        System.out.print("请输入您的的选择：");
    }
    /*//查询按时完成次数
    public static void  frequency_t(User u,UserService userService){
        System.out.println("...............按时完成次数查询...............");
        int frequency = userService.frequency(u);
        System.out.println("你的余额为："+frequency+"次！");
        //回车继续
        TSUtility.readReturn();
        System.out.println("1.个人信息修改\t\t\t2.查询用户等级");
        System.out.println("3.查询余额    \t\t\t4.返回上一级目录");
        System.out.print("请输入您的的选择：");
    }*/

    //兼职申请
    private static void apply(User user,UserService userService){
        while (true) {
            //兼职申请功能视图
            View.ApplyJobView();
            char c = TSUtility.readMenuSelectionPro();
            switch (c) {
                case '1':
                    //查询所有职位信息
                    query_position_information(userService);
                    break;
                case '2':
                    //查询可申请职位信息
                    query_BLA_information(user,userService);
                    break;
                case '3':
                    //申请职位
                    application(user,userService);
                    break;
                case '4':
                    underway(user,userService);
                    //查询正在兼职的信息
                    break;
                case '5':
                    //查询已完成兼职
                    finish(user,userService);
                    break;
                case '6':
                    //结束工作
                    end(user,userService);
                    break;
                case '7':
                    //返回上一目录
                    System.out.println("\t\t1.个人信息管理\n\t\t2.兼职申请\n\t\t3.退出登录\n\t\t4.退出系统");
                    System.out.print("请输入您的选择：");
                    return;
            }
        }
    }

    //查询所有职位信息
    private static void query_position_information(UserService userService){
        System.out.println("++++++++++查询所有现有职位信息++++++++++");
        List<Job> jobs = userService.position_information();
        if (jobs.size()==0){
            System.out.println("暂无兼职信息！\n");
            return;
        }
        Manager_menu.DY(jobs);
        System.out.println("-------------------------------------------------------------------------------------------------------------------------\n");
    }

    private static List<Job> information(UserService userService,User user){
        List<Job> jobs = userService.position_information();
        if (jobs.size()==0){
            System.out.println("暂无兼职信息！\n");
            return null;
        }
        List<Job> jobList=new ArrayList<Job>();
        for (Job job:jobs){
            if (job.getDemand()!=0&&job.getJob_level()<=user.getCredit_rating()){
                //获取当前时间毫秒值
                long time=System.currentTimeMillis();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH：mm");
                //获取兼职结束时间
                long time1=0;
                try {
                    time1=sdf.parse(job.getJob1_time()).getTime();
                }catch (Exception e){
                    e.fillInStackTrace();
                }
                time=time1/1000/60/60-time/1000/60/60;
                if (time>1)
                    jobList.add(job);
            }
        }
        return jobList;
    }

    //查询可申请职位信息
    private static void query_BLA_information(User user,UserService userService){
        System.out.println("+++++++++++所有可申请职位信息查询+++++++++++++");
        System.out.println("\t提示：只展示在兼职时间到结束前1小时，不足一小时不在展示！");
        List<Job> jobList = information(userService, user);
        if (jobList==null)
            return;
        Manager_menu.DY(jobList);
        System.out.println("-------------------------------------------------------------------------------------------------------------------------\n");
    }

    //申请职位
    private static void application(User user,UserService userService){
        System.out.println("***********申请职位************");
        System.out.println("\t提示：在兼职时间到结束前1小时均可申请，不足一小时不可申请；" +
                "\n工资按小时计算，超时未完成按对应超时扣费比例进行扣除相应的工资\n");
        List<Job> jobList = information(userService, user);
        if (jobList==null)
            return;
        if (userService.level(user)==0){
            System.out.println("你的信誉等级为：0级，不能申请任何工作！\n");
            return;
        }
        int did=TSUtility.did();
        int in=0;
        long job_time=0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH：mm");
        Job job1 = null;
        for (Job job:jobList){
            if (job.getDID()==did) {
                in++;
                job1=job;
                job1.setDemand(job.getDemand()-1);
                try {
                    job_time=sdf.parse(job.getJob_time()).getTime();
                }catch (Exception e){
                    e.fillInStackTrace();
                }
                break;
            }
        }
        if (in==0){
            System.out.println("你输入的可申请兼职编号不存在！\n");
            return;
        }
        boolean b = userService.repeat(user.getACID(), did);
        if (b){
            System.out.println("该职位你正在进行中，请勿重复操作！\n");
            return;
        }
        List<Job> jobs = userService.processing(user.getACID());
        if (jobs!=null) {
            long job1_time=0;long time=0;
            for (Job job : jobs) {
                try {
                    job1_time = sdf.parse(job.getJob1_time()).getTime();
                } catch (Exception e) {
                    e.fillInStackTrace();
                }
                time = job1_time - job_time;
                if (time > 0) {
                    System.out.println("你工作时间段已有工作，发生冲突，不能申请该工作！\n");
                    return;
                }
            }
        }
        job1.setUid(user.getUid());
        job1.setJob_acid(user.getACID());
        job1.setStare("true");
        userService.add_worker(job1);
        System.out.println("兼职申请成功！\n");
    }

    //查询正在进行中的工作
    private static void underway(User user,UserService userService){
        System.out.println("***********查询正在兼职的信息************");
        List<Job> jobs = userService.processing(user.getACID());
        if (jobs.size()==0){
            System.out.println("暂无正在进行中的兼职信息！\n");
            return;
        }
        ZS(jobs);
        System.out.println("----------------------------------------------------------------------------------------");
    }

    private static void ZS(List<Job> jobs){
        System.out.println("兼职编号\t公司名称\t公司编号 兼职类型 职位名称 发布兼职人员id 职位等级 工资（元/小时）\t工作开始时间\t\t截至时间\t\t完成时间\t逾期扣费比例\t工作描述");
        for (Job job:jobs){
            System.out.println(job.getDID()+"\t   "+job.getCompany_name()+"\t"+job.getJID()+"\t"+job.getJobType_name()+"\t "+job.getJob_name()+"\t\t"
                    +job.getAcid()+"\t\t"+job.getJob_level()+"\t\t"+job.getSalary()+"\t\t"+job.getJob_time()+" "+job.getJob1_time()+" "+job.getTime()
                    +"  "+job.getDeduct_salary() +job.getJob_content());
        }
    }

    //查询已完成兼职信息
    private static void finish(User user,UserService userService){
        System.out.println("***********查询已完成的兼职信息************");
        List<Job> jobs = userService.finish(user.getACID());
        if (jobs.size()==0){
            System.out.println("暂无已完成兼职信息！\n");
            return;
        }
        ZS(jobs);
        System.out.println("----------------------------------------------------------------------------------------");
    }

    //结束工作
    private static void end(User user,UserService userService){
        System.out.println("------------结束正在进行中工作------------");
        List<Job> jobs = userService.processing(user.getACID());
        if (jobs.size()==0){
            System.out.println("暂无正在进行中的兼职！\n");
            return;
        }
        //System.out.print("请输入兼职编号：");
        int did=TSUtility.did();int in=0;Job job1=null;
        long job_time=0;long job1_time=0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH：mm");
        for (Job job:jobs){
            if (job.getDID()==did){
                in++;
                job1=job;
                try {
                    job_time=sdf.parse(job.getJob_time()).getTime();
                    job1_time=sdf.parse(job.getJob1_time()).getTime();
                }catch (Exception e){
                    e.fillInStackTrace();
                }
                break;
            }
        }
        if (in==0){
            System.out.println("你输入的可申请兼职编号不存在！");
            return;
        }
        User user1 = userService.id(user.getACID());
        int frequency=user1.getFrequency();
        int frequency_t=user1.getFrequency_t();
        int credit_rating=user1.getCredit_rating();
        //获取当前时间毫秒值
        long time=System.currentTimeMillis();
        double salary=0;
        long time1=time/1000/60/60-job1_time/1000/60/60;
        if (time1>0){
            long time2=job1_time/1000/60/60-job_time/1000/60/60;
            System.out.println("你已超时，按照对应的超时扣费比例，应扣除："+time1*job1.getDeduct_salary()+"元");
            salary=job1.getSalary()*time2-time1*job1.getDeduct_salary();
            System.out.println("扣除超时费用后你的工资为："+salary+"元");
            frequency_t++;
            if (frequency_t==3){
                frequency_t=0;
                credit_rating--;
            }
        }else {
            long time2=time/1000/60/60-job_time/1000/60/60;
            salary=job1.getSalary()*time2;
            System.out.println("你本次工作未按时完成，工资为："+salary+"元");
            frequency++;
            if (frequency==3){
                frequency=0;
                credit_rating++;
                if (credit_rating>5)
                    credit_rating=5;
            }
        }
        user1.setFrequency(frequency);
        user1.setFrequency_t(frequency_t);
        user1.setCredit_rating(credit_rating);
        //结束工作根据实际情况更新用户信息
        userService.modify(user1);
        userService.user_balance(user.getACID(),salary);
        userService.update_end(did);
        if (job1.getDemand()==0){
            userService.update(did);
        }
        System.out.println("结束工作成功！\n");
    }
}
